package com.zzxy.spring12.factory;

import com.zzxy.spring12.aspect.MyAspect;
import com.zzxy.spring12.service.impl.StudentService;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class MyBeanFactory {
     public static StudentService creatStudentService() {
         StudentService studentService = new StudentService();
         MyAspect myAspect = new MyAspect();
         StudentService studentServiceProxy = (StudentService) Proxy.newProxyInstance(MyBeanFactory.class.getClassLoader(), studentService.getClass().getInterfaces(), new InvocationHandler() {
             @Override
             public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                 myAspect.before();
                 myAspect.after();
               Object obj = method.invoke(studentService, args);
                 return obj;
             }
         });
           return studentServiceProxy ;
     }
        }
